package com.wd.jetpack.ui.main

import android.view.View
import androidx.databinding.ViewDataBinding
import com.google.gson.Gson
import com.wd.base.util.Logger
import com.wd.jetpack.R
import com.wd.jetpack.annotation.SingleClick
import com.wd.jetpack.base.BaseActivity
import com.wd.jetpack.base.SingleItemClickPresenter
import com.wd.jetpack.databinding.ActivityListBinding
import com.wd.jetpack.databinding.ItemListLayoutBinding
import com.wd.jetpack.helper.exytens.bindLifeCycle
import com.wd.jetpack.ui.viewmodel.ListItemViewModel
import com.wd.jetpack.ui.viewmodel.ListViewModel
import io.ditclear.bindingadapter.BindingViewHolder
import io.ditclear.bindingadapter.ItemDecorator
import io.ditclear.bindingadapter.SingleTypeAdapter

/**
 * NA<BR>
 *
 * @author w4425
 * @version [V5.9.3, 2021/6/17]
 * @since V5.9.3
 */
class ListActivity : BaseActivity<ActivityListBinding>(), SingleItemClickPresenter<ListItemViewModel> {
    private val mViewModel: ListViewModel by lazy { ListViewModel() }

    private val adapter by lazy {
        SingleTypeAdapter(mContext, R.layout.item_list_layout, mViewModel.items).apply {
            itemDecorator = object : ItemDecorator {
                override fun decorator(holder: BindingViewHolder<ViewDataBinding>, position: Int, viewType: Int) {
                    val binding = holder.binding as ItemListLayoutBinding
                    //演示自定义点击事件
                    binding.picClick = onItemClickThumbListener
                }
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_list
    }

    override fun initView() {
        mBinding.vm = mViewModel
        mBinding.adapter = adapter
        adapter.itemPresenter = this
    }

    override fun initData() {
        getArticleData()
    }

    /**
     * subscribe 添加订阅
     */
    private fun getArticleData() {
        mViewModel.getArticle().bindLifeCycle(this)//绑定声明周期
                .subscribe({
                    //成功回调
                    Logger.e("---------------------------")
                    Logger.e(Gson().toJson(it.data))
                }, {
                    //失败回调
                })
    }


    @SingleClick
    override fun onClick(v: View) {
    }

    override fun onItemClick(v: View, item: ListItemViewModel) {
        Logger.e("点击行：${Gson().toJson(item)}")
    }


    private val onItemClickThumbListener = object : OnItemClickThumbListener {
        override fun onClickThumb(v: View, item: ListItemViewModel) {
            Logger.e("点击图片：${Gson().toJson(item)}")
        }

    }    //演示自定义点击事件

    interface OnItemClickThumbListener {
        fun onClickThumb(v: View, item: ListItemViewModel)
    }
}